package ut.data;
import java.util.Date;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
public class AbuseReport{
	// State
	public static final int PROCESSING = 0;
	public static final int PROCESSED = 1;
	// Task Related Abuse Report
	public static final int TASK = 1;
	public static final int FREE_TASK = 2;
	// User Related Abuse Report
	public static final int EMPLOYER = 11;
	public static final int TRANSLATOR = 12;
	public static boolean createAbuseReport(int reporter, int type, long target){
		if(Database.executeUpdate("insert into abuse_report(reporter,type,target,time) values(" + reporter + "," + type + "," + target + ",now())") == 1){
			ut.service.Mail.sendAbuseReportEmail();
			return true;
		}else{
			return false;
		}
	}
	// 標示為已經處理
	public static boolean abuseReportProcessed(long id){
		return (Database.executeUpdate("update abuse_report set state=" + AbuseReport.PROCESSED + " where id=" + id) == 1);
	}
	// 取得尚未處理的回報紀錄最大頁數 (管理者)
	public static int getMaxAbuseReportPage(int pageSize){
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{
			con = Database.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select count(*) from abuse_report where state=" + AbuseReport.PROCESSING);
			rs.next();
			return ((rs.getInt("count(*)") - 1) / pageSize) + 1;
		}catch(NamingException e){
			e.printStackTrace();
			return -1;
		}catch(SQLException e){
			e.printStackTrace();
			return -1;
		}finally{
			try{
				if(con != null){
					con.close();
				}
				if(stmt != null){
					stmt.close();
				}
				if(rs != null){
					rs.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
				return -1;
			}
		}
	}
	// 取得所有歷史交易紀錄列表 (管理者)
	public static AbuseReport[] getAbuseReports(int pageSize, int pageNumber){
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{

			con = Database.getConnection();
			stmt = con.createStatement();

			AbuseReport[] reports = new AbuseReport[pageSize];
			rs = stmt.executeQuery("select * from abuse_report where state=" + AbuseReport.PROCESSING + " order by time desc limit " + ((pageNumber-1)*pageSize) + "," + pageSize);
			int index = 0;
			while(rs.next()){
				reports[index] = new AbuseReport(rs.getLong("id"), rs.getInt("reporter"), rs.getInt("type"), rs.getInt("target"), rs.getTimestamp("time"), rs.getInt("state"));
				index++;
			}
			return reports;
		}catch(NamingException e){
			e.printStackTrace();
			return null;
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
			try{
				if(con != null){
					con.close();
				}
				if(stmt != null){
					stmt.close();
				}
				if(rs != null){
					rs.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
				return null;
			}
		}
	}
	/* Instance Definition */
	public long id;
	public int reporter;
	public int type;
	public long target;
	public Date time;
	public int state;
	public AbuseReport(long id, int reporter, int type, long target, Date time, int state){
		this.id = id;
		this.reporter = reporter;
		this.type = type;
		this.target = target;
		this.time = time;
		this.state = state;
	}
}